/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.model;

import com.jb.framework.PersistentDescriptor;

import java.util.ArrayList;
import java.util.List;


public class DBDescriptor implements PersistentDescriptor {

    List list = new ArrayList();

    public int getFieldLength(String fieldName) {
        if (fieldName.equals("description")) {
            return 1024;
        } else if (fieldName.equals("link")) {
            return 1024;
        } else if (fieldName.equals("fileLocation")) {
            return 1024;
        } else {
            return 64;
        }
    }

    public String getFieldName(String fieldName) {
        return fieldName;
    }

    public String getFieldType(String javaType, String fieldName) {
        if (javaType.equals("int")) {
            return "int";
        }

        if (javaType.equals("long")) {
            return "int(32)";
        }

        if (javaType.equals("boolean")) {
            return "VARCHAR(6)";
        }

        if (javaType.equals("byte")) {
            return "int(3)";
        }

        if (javaType.equals("short")) {
            return "int(12)";
        }

        if (javaType.equals("char")) {
            return "VARCHAR2(1)";
        }

        if (javaType.equals("float")) {
            return "float";
        }

        if (javaType.equals("class java.util.Calendar")) {
            return "DATE";
        }

        if (javaType.equals("double")) {
            return "double";
        } else if (javaType.equals("class java.lang.String")) {
            if (getFieldLength(fieldName) < 255) {
                return "VARCHAR(" + getFieldLength(fieldName) + ")";
            } else {
                return "VARCHAR(125)";
            }
        } else if (javaType.equals("class java.util.Date")) {
            return "DATE";
        } else {
            return "VARCHAR(64)";
        }
    }

    public List getIndexes() {
        return list;
    }

    public boolean isPersistable(String fieldName) {
        return true;
    }

    public boolean isPrimaryKey(String fieldName) {
        return "id".equals(fieldName);
    }

    protected void addIndex(String name, String[] fields) {
        list.add(new IndexDescriptor("", fields, null));
    }

    protected void addUniqueIndex(String name, String[] fields) {
        list.add(new IndexDescriptor("unique", fields, null));
    }
}
